package com.shujia.compute

import java.util.Properties
import java.util.concurrent.TimeUnit

import com.google.gson.Gson
import com.shujia.async.CarJoinKcAsync
import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.connectors.kafka.{FlinkKafkaConsumer, FlinkKafkaProducer, KafkaSerializationSchema}
import com.shujia.bean.Car
import org.apache.flink.contrib.streaming.state.RocksDBStateBackend
import org.apache.flink.runtime.state.StateBackend
import org.apache.flink.streaming.api.CheckpointingMode
import org.apache.flink.streaming.api.environment.CheckpointConfig.ExternalizedCheckpointCleanup
import org.apache.flink.streaming.connectors.kafka.internals.KeyedSerializationSchemaWrapper

object CarJoinKcToKafka {
  def main(args: Array[String]): Unit = {

    val env = StreamExecutionEnvironment.getExecutionEnvironment
    // 每 1000ms 开始一次 checkpoint// 每 1000ms 开始一次 checkpoint
    /*env.enableCheckpointing(1000)
    // 高级选项：

    // 设置模式为精确一次 (这是默认值)
    env.getCheckpointConfig.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE)

    // 确认 checkpoints 之间的时间会进行 500 ms
    env.getCheckpointConfig.setMinPauseBetweenCheckpoints(500)

    // Checkpoint 必须在一分钟内完成，否则就会被抛弃
    env.getCheckpointConfig.setCheckpointTimeout(60000)

    // 同一时间只允许一个 checkpoint 进行
    env.getCheckpointConfig.setMaxConcurrentCheckpoints(1)

    // 开启在 job 中止后仍然保留的 externalized checkpoints
    env.getCheckpointConfig.enableExternalizedCheckpoints(ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION)

    //设置checkpoint 保存位置为外部持久化系统
    val rocksDBStateBackend: StateBackend = new RocksDBStateBackend("hdfs://node1:9000/flink/checkpoint", true)
    env.setStateBackend(rocksDBStateBackend)
*/
    env.setParallelism(1)

    val kafkaProps = new Properties()
    kafkaProps.setProperty("zookeeper.connect", "node1:2181")
    kafkaProps.setProperty("bootstrap.servers", "node1:9092")
    kafkaProps.setProperty("group.id", "qweqwew")

    val kafkaConsumer = new FlinkKafkaConsumer[String]("ods_car", new SimpleStringSchema, kafkaProps)
    //从头读取数据
    kafkaConsumer.setStartFromEarliest()
    val lineDS = env.addSource(kafkaConsumer)

    val carDS = lineDS.map(line => {
      val gson = new Gson()
      gson.fromJson(line, classOf[Car])
    })

    //关联卡口, 道路, 区域

    //异步io
    //orderedWait 返回结果有序
    //unorderedWait  先查询到先返回

    val joinDS = AsyncDataStream.unorderedWait(
      carDS, //需要关联的流
      new CarJoinKcAsync(), //自定义异步方法
      20, //么一个请求超时时间
      TimeUnit.SECONDS, //
      100 //异步数量
    )


    val jsonDS = joinDS.map(carWide => {
      val gson = new Gson()
      gson.toJson(carWide)
    })

    val sinProperties = new Properties()
    sinProperties.setProperty("bootstrap.servers", "node1:9092")
    sinProperties.setProperty("zookeeper.connect", "node1:2181")
    //设置为小于15分钟
    sinProperties.setProperty("transaction.timeout.ms", 14 * 60 * 1000 + "")


    //将数据保存到kafka
    val kafkaSink = new FlinkKafkaProducer[String](
      "dwd_car_wide",
      new KeyedSerializationSchemaWrapper[String](new SimpleStringSchema),
      sinProperties,
      FlinkKafkaProducer.Semantic.EXACTLY_ONCE)

    jsonDS.addSink(kafkaSink)

    env.execute("join")

  }
}
